package com.soft.face.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.soft.face.config.system.ServerConfig;
import com.soft.face.config.system.SystemConfig;
import com.soft.face.entity.ArcSoft;
import com.soft.face.entity.dto.ArcSoftDTO;
import com.soft.face.entity.dto.ArcSoftInsertDTO;
import com.soft.face.global.BusinessException;
import com.soft.face.mapper.ArcSoftMapper;
import com.soft.face.service.ArcSoftService;
import com.soft.face.service.UserFaceService;
import com.soft.face.utils.file.FileUploadUtils;
import com.soft.face.utils.file.FileUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.File;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipFile;

@Service
@AllArgsConstructor
public class ArcSoftServiceImpl extends ServiceImpl<ArcSoftMapper, ArcSoft> implements ArcSoftService {

    private UserFaceService userFaceService;
    private ServerConfig serverConfig;

    @Override
    public PageInfo<ArcSoft> list(ArcSoftDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        return new PageInfo<>(this.list());
    }

    @Override
    public void insert(ArcSoftInsertDTO dto) throws Exception {
        // 上传文件路径
        String filePath = SystemConfig.getUploadPath();
        String libarcsoftFace = serverConfig.getUrl() + FileUploadUtils.upload(filePath, FileUtils.getMultipartFile(dto.getLibarcsoftFace().getInputStream(), dto.getLibarcsoftFace().getOriginalFilename()));
        String libarcsoftFaceEngine = serverConfig.getUrl() + FileUploadUtils.upload(filePath, FileUtils.getMultipartFile(dto.getLibarcsoftFaceEngine().getInputStream(), dto.getLibarcsoftFaceEngine().getOriginalFilename()));
        String libarcsoftFaceEngineJni = serverConfig.getUrl() + FileUploadUtils.upload(filePath, FileUtils.getMultipartFile(dto.getLibarcsoftFaceEngineJni().getInputStream(), dto.getLibarcsoftFaceEngineJni().getOriginalFilename()));
        String simple = serverConfig.getUrl() + FileUploadUtils.upload(filePath, FileUtils.getMultipartFile(dto.getSimple().getInputStream(), dto.getSimple().getOriginalFilename()));
        ArcSoft arcSoft = new ArcSoft();
        arcSoft.setId(IdUtil.simpleUUID());
        arcSoft.setVersion(dto.getVersion());
        arcSoft.setAppId(dto.getAppId());
        arcSoft.setAppKey(dto.getAppKey());
        arcSoft.setLibarcsoftFace(libarcsoftFace);
        arcSoft.setLibarcsoftFaceEngine(libarcsoftFaceEngine);
        arcSoft.setLibarcsoftFaceEngineJni(libarcsoftFaceEngineJni);
        arcSoft.setSimpleUrl(simple);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date = format.format(new Date());
        arcSoft.setCreateDate(date);
        arcSoft.setUpdateDate(date);
        // 存储人脸图像数据
        String name = dto.getSimple().getOriginalFilename();
        if (!name.endsWith(".zip")) {
            throw new BusinessException("素材库文件必须是zip格式");
        }
        File tempFile = File.createTempFile(IdUtil.fastUUID(), ".zip");
        File file = FileUtil.writeFromStream(dto.getSimple().getInputStream(), tempFile);
        ZipFile zipFile = new ZipFile(file, Charset.forName("gbk"));
        userFaceService.upload(zipFile);
        userFaceService.cache();
        this.save(arcSoft);
    }

    @Override
    public void delete(String id) {
        this.removeById(id);
    }

    @Override
    public JSONObject task(String id) throws Exception {
        // TODO 由于时间原因无法简单的实现分布式计算 原理调用其他模块去运行
        List<Float> list = Arrays.asList(0.5f, 1.0f, 1.5f, 2.0f, 2.5f, 3.0f, 3.5f, 4.0f, 4.5f, 5.0f, 5.5f, 6.0f, 6.5f, 7.0f, 7.5f, 8.0f, 8.5f, 9.0f, 9.5f);
        return userFaceService.analyse(list);
    }
}
